<?php 
require_once("admin-header.php");
require_once("../maincore.php");
?>
<meta http-equiv="Content-Type" content="text/html; charset=utf8">
<title>Thêm kì thi</title>

<?php if (isset($_POST['syear']))
{
	
	require_once("../include/db_info.inc.php");
	//require_once("../include/check_post_key.php");
	$starttime=intval($_POST['syear'])."-".intval($_POST['smonth'])."-".intval($_POST['sday'])." ".intval($_POST['shour']).":".intval($_POST['sminute']).":00";
	$endtime=intval($_POST['eyear'])."-".intval($_POST['emonth'])."-".intval($_POST['eday'])." ".intval($_POST['ehour']).":".intval($_POST['eminute']).":00";
	//	echo $starttime;
	//	echo $endtime;

	$title=$_POST['title'];
	$private=$_POST['private'];
	if (get_magic_quotes_gpc ()){
		$title = stripslashes ($title);
		$private = stripslashes ($private);
	}
	$title=mysql_real_escape_string($title);
	$private=mysql_real_escape_string($private);
	
    $lang=$_POST['lang'];
    $langmask=$OJ_LANGMASK;
    /**
	foreach($lang as $t){
			$langmask+=1<<$t;
	} 
	$langmask=1023&(~$langmask);
	**/
	//echo $langmask;	
	
	$sql="INSERT INTO `contest`(`title`,author_id,`start_time`,`end_time`,`private`,`langmask`)
		VALUES('$title','$myid','$starttime','$endtime','$private',$langmask)";
//	echo $sql;
	mysql_query($sql) or die(mysql_error());
	$cid=mysql_insert_id();
	echo "Thêm kì thi ".$cid;
	$sql="DELETE FROM `contest_problem` WHERE `contest_id`=$cid";
	$plist=trim($_POST['cproblem']);
	$plist = str_replace(" ","",$plist);
	$pieces = explode(",",$plist );
	if (count($pieces)>0 && strlen($pieces[0])>0){
		$sql_1="INSERT INTO `contest_problem`(`contest_id`,`problem_id`,`num`) 
			VALUES ('$cid','$pieces[0]',0)";
		for ($i=1;$i<count($pieces);$i++){
			$sql_1=$sql_1.",('$cid','$pieces[$i]',$i)";
		}
		echo $sql_1;
		mysql_query($sql_1) or die(mysql_error());
		$sql="update `problem` set defunct='N' where `problem_id` in ($plist)";
		mysql_query($sql) or die(mysql_error());
	}
	$sql="DELETE FROM `privilege` WHERE `rightstr`='c$cid'";
	mysql_query($sql);
	$p_ulist = $_POST['ulist'];
	$p_ulist = str_replace("\n",",",$p_ulist);
	$p_ulist = str_replace(" ","",$p_ulist);
	$p_ulist = str_replace(",,",",",$p_ulist);
	$pieces = explode(",", trim($p_ulist));
	if (count($pieces)>0 && strlen($pieces[0])>0){
		$sql_1="INSERT INTO `privilege`(`user_id`,`rightstr`) 
			VALUES ('".trim($pieces[0])."','c$cid')";
		for ($i=1;$i<count($pieces);$i++)
			$sql_1=$sql_1.",('".trim($pieces[$i])."','c$cid')";
		//echo $sql_1;
		mysql_query($sql_1) or die(mysql_error());
	}
} else {
   if(isset($_GET['cid'])){
		   $cid=intval($_GET['cid']);
		   $sql="select * from contest WHERE `contest_id`='$cid'";
		   $result=mysql_query($sql) or die(mysql_error());
		   $row=mysql_fetch_object($result);
		   $title=$row->title;
		   mysql_free_result($result);
			$plist="";
			$sql="SELECT `problem_id` FROM `contest_problem` WHERE `contest_id`=$cid ORDER BY `num`";
			$result=mysql_query($sql) or die(mysql_error());
			for ($i=mysql_num_rows($result);$i>0;$i--){
				$row=mysql_fetch_row($result);
				$plist=$plist.$row[0];
				if ($i>1) $plist=$plist.',';
			}
			mysql_free_result($result);
   }
else if(isset($_POST['problem2contest'])){
	   $plist="";
	   //echo $_POST['pid'];
	   sort($_POST['pid']);
	   foreach($_POST['pid'] as $i){		    
			if ($plist) 
				$plist.=','.$i;
			else
				$plist=$i;
	   }
}else if(isset($_GET['spid'])){
	//require_once("../include/check_get_key.php");
		   $spid=intval($_GET['spid']);
		 
			$plist="";
			$sql="SELECT `problem_id` FROM `problem` WHERE `problem_id`>=$spid ";
			$result=mysql_query($sql) or die(mysql_error());
			for ($i=mysql_num_rows($result);$i>0;$i--){
				$row=mysql_fetch_row($result);
				$plist=$plist.$row[0];
				if ($i>1) $plist=$plist.',';
			}
			mysql_free_result($result);
}  
  
?>

	<form method=POST action='<?php echo MYSELF?>'>
	<p align=center><h1>Thêm một kì thi mới</h1></p>
	<p align=left>Tiêu đề: <input type='text' name='title' size='71' value="<?php echo isset($title)?$title:""?>"></p>
	<p align=left>Bắt đầu lúc:<br>&nbsp;&nbsp;&nbsp;
	Năm: <input type='text' name='syear' value=<?php echo date('Y')?> size='7' >
	Tháng: <input type='text' name='smonth' value=<?php echo date('m')?> size='7' >
	Ngày: <input type='text' name='sday' size='7' value=<?php echo date('d')?> >&nbsp;
	Giờ: <input type='text' name='shour' size='7' value=<?php echo date('H')?>>&nbsp;
	Phút: <input type='text' name='sminute' value='00' size='7' ></p>
	<p align='left'>Kết thúc lúc:<br>&nbsp;&nbsp;&nbsp;
	Năm:<input type='text' name='eyear' value=<?php echo date('Y')?> size='7' >
	Tháng:<input type='text' name='emonth' value=<?php echo date('m')?> size='7' >
	
	Ngày:<input type='text' name='eday' size='7' value=<?php echo date('d')+(date('H')+4>23?1:0)?>>&nbsp;
	Giờ:<input type='text' name='ehour' size='7' value=<?php echo (date('H')+4)%24?>>&nbsp;
	Phút:<input type='text' name='eminute' value='00' size='7' ></p>
	
	<p>Chế độ: <select name='private'><option value='0'>Công khai</option><option value='1' selected>Bảo mật</option></select>
	<i>Chỉ giới hạn một số thí sinh hay cho <b>tất cả thí sinh</b> tham gia. </i>
	</p>
	<!--
	Ngôn ngữ lập trình được dùng:<select name="lang[]" multiple>
		<option value=0 selected>C</option>
		<option value=1 selected>C++</option>
		<option value=2 selected>Pascal</option>
		<option value=3 selected>Java</option>	
		<option value=4 selected>Ruby</option>	
		<option value=5 selected>Bash</option>	
		<option value=6 selected>Python</option>	
		<option value=7 selected>PHP</option>	
		<option value=8 selected>Perl</option>	
		<option value=9 selected>C#</option>
	</select>
	-->
	<?php require_once("../include/set_post_key.php");?>
	<p>Danh mục đề bài: <input type=text size=60 name=cproblem value="<?php echo isset($plist)?$plist:""?>">. <i>Ví dụ: 1001,1002,1004</i></p>
	<p>
	Những thí sinh được tham gia: <i>Ví dụ: hs1,hs2,hs4</i><br><textarea name="ulist" rows="5" cols="80"></textarea>. 
	</p>
	<br />
	<p><input type='submit' value='Thêm kì thi' name='submit'><input type='reset' value='Hủy' name='reset'></p>
	</form>
<?php }
require_once("../oj-footer.php");

?>

